package com.hfi.ood.payroll.schedule;

import com.hfi.ood.payroll.domain.PaymentSchedule;

import java.time.DayOfWeek;
import java.time.Instant;
import java.time.LocalDate;
import java.time.ZoneId;
import java.time.temporal.ChronoUnit;
import java.time.temporal.TemporalAdjusters;
import java.util.Date;

/**
 * @author ChangLiang
 * @date 2020/5/19
 */
public class WeeklySchedule implements PaymentSchedule {
    @Override
    public boolean isPayDay(Date date) {
        Instant instant = Instant.ofEpochMilli(date.getTime());
        LocalDate current = instant.atZone(ZoneId.systemDefault()).toLocalDate();
        LocalDate friday = current.with(TemporalAdjusters.nextOrSame(DayOfWeek.FRIDAY));
        return current.equals(friday);
    }

    @Override
    public Date getPayPeriodStartDate(Date payPeriodEndDate) {
        LocalDate current = Instant.ofEpochMilli(payPeriodEndDate.getTime()).atZone(ZoneId.systemDefault()).toLocalDate();
        return Date.from(current.plus(-6, ChronoUnit.DAYS).atStartOfDay(ZoneId.systemDefault()).toInstant());
    }
}
